package com.liujie.dhunting.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liujie.dhunting.utils.Result;
import com.liujie.dhunting.entity.User;
import com.liujie.dhunting.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserMapper userMapper;


    @PostMapping
    public Result<?> save(@RequestBody User user){
        int p=userMapper.insert(user);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody User user){
        int p=userMapper.updateById(user);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        int p= userMapper.deleteById(id);
        return Result.success();
    }
    @GetMapping("/find")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNumber,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String keyWord){
        LambdaQueryWrapper<User> wrapper=Wrappers.<User>lambdaQuery();
        if(StrUtil.isNotBlank(keyWord)){
            wrapper.like(User::getNickName,keyWord);
        }
        Page<User> userPage = userMapper.selectPage(new Page<User>(pageNumber,pageSize), wrapper);
        return Result.success(userPage);
    }
}
